#include <iostream>
#include <fstream>
#include "AbstractTree.h"
#include "parser.hpp"
#include "CodeGenContext.h"
#include "errorhandle.h"
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/FileSystem.h>
//新增内容
/*#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"*/

extern int yyparse();
extern AbstractTree::ProgramNode* astRoot;
int ParseError=0;
FILE* file;

int main()
{
    file = fopen("pas/3.pas", "r");
    std::error_code EC;
    llvm::raw_fd_ostream ir("pas/3.ll", EC, llvm::sys::fs::F_None);
    init_error_handle();
    yyparse();
    close_error_handle();
    if (ParseError) return 0;
    CodeGenContext context;
    astRoot->CodeGen(context);
    std::cout << std::flush;
    context.Builder.CreateRetVoid();
    context.module->print(llvm::errs(), nullptr);
    context.module->print(ir, nullptr);
    //新增内容，编译时请删除
    /* 输出汇编文件
    std::error_code errorCode;
    std::string filename = "pas/1.s";  // 汇编文件名
    llvm::raw_fd_ostream dest(filename, errorCode, llvm::sys::fs::F_None);
    llvm::legacy::PassManager pass;
    //llvm::CodeGenFileType type = llvm::CGFT_AssemblyFile;  // 文件类型为汇编
    // llvm::CodeGenFileType type = llvm::CGFT_ObjectFile;  // 文件类型为可执行文件
    //theTargetMachine->addPassesToEmitFile(pass, dest, nullptr, type);
    //pass.run(*mod);
    context.module->print(dest, nullptr);
    //dest.flush();
    //mod->print(llvm::outs(), nullptr);  */
    return 0;
}
